/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

/**
 * This class intended to be used for inquiring node status, restarting, etc.
 * Right now only the abilities to stop, start and restart syslog threads have
 * been implemented.
 * 
 * Note: Not yet fully implemented.
 * 
 * @author bduphoff
 * 
 */

public class ControlSocket extends Thread {

    /***************************************************************************
     * Operation to start a NetFlow thread.
     */
    public static final int OP_START_NETFLOW = 1;

    /***************************************************************************
     * Operation to stop a NetFlow thread.
     */
    public static final int OP_STOP_NETFLOW = 2;

    /***************************************************************************
     * Operation to restart a NetFlow thread.
     */
    public static final int OP_RESTART_NETFLOW = 3;

    /***************************************************************************
     * Operation to start a syslog thread.
     */
    public static final int OP_START_SYSLOG = 4;

    /***************************************************************************
     * Operation to stop a syslog thread.
     */
    public static final int OP_STOP_SYSLOG = 5;

    /***************************************************************************
     * Operation to restart a syslog thread.
     */
    public static final int OP_RESTART_SYSLOG = 6;

    /***************************************************************************
     * The socket associated with this object. Connection to whatever client
     * initiated the connection to the ControlServer.
     */
    private Socket socket;

    /***************************************************************************
     * Simple constructor just assigns the socket that is used to communicate
     * with the connecting client.
     * 
     * @param s
     *            The socket to be associated with this object
     * 
     */
    public ControlSocket(Socket s) {
        this.socket = s;
    }

    /***************************************************************************
     * The control socket can be used to start, stop and restart syslog data
     * collection threads. Only localhost can connect to the conrol socket.
     * 
     * ToDo: Implement NetFlow control capabilities.
     * 
     */
    public void run() {
        try {

            // Only the loopback interface can talk to the control socket.
            if (!socket.getInetAddress().isLoopbackAddress()) {
                // If not the loopback, return
                socket.close();
                return;
            }

            DataInputStream dis = new DataInputStream(socket.getInputStream());
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            int sensorID = dis.readInt(); // read the sensor ID
            int opType = dis.readInt(); // Read the op type, on of the OP_
                                        // constants above

            SyslogThread st = null;
            MetaDatabase mdb = null;

            switch (opType) {
            case ControlSocket.OP_START_SYSLOG:
                mdb = MetaDatabase.create();
                st = mdb.getSyslogThread(sensorID);
                if (st == null) {
                    mdb.closeConnection();
                    return;
                }
                mdb.closeConnection();
                NetFSEServer.addSyslogThread(st);
                st.start();
                break;
            case ControlSocket.OP_STOP_SYSLOG:
                SyslogThread st2 = NetFSEServer.removeSyslogThread(sensorID);
                break;
            case ControlSocket.OP_RESTART_SYSLOG:
                st = NetFSEServer.removeSyslogThread(sensorID);
                mdb = MetaDatabase.create();
                st = mdb.getSyslogThread(sensorID);
                if (st == null) {
                    mdb.closeConnection();
                    return;
                }
                mdb.closeConnection();
                NetFSEServer.addSyslogThread(st);
                st.start();
                break;
            case ControlSocket.OP_START_NETFLOW:
                // Need to implement this as per syslog
                break;
            case ControlSocket.OP_STOP_NETFLOW:
                // Need to implement this as per syslog
                break;
            case ControlSocket.OP_RESTART_NETFLOW:
                // Need to implement this as per syslog
                break;
            }

            dos.writeInt(-1);
            dos.close();
            dis.close();
            socket.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}